﻿using System;

using Common.Logging;
using System.IO;

namespace Quartz.Examples.Example15
{
    /// <summary>
    /// This is just a simple job that gets fired off many times by example 15.
    /// </summary>
    /// <author>Bill Kratzer</author>
    /// <author>Marko Lahma (.NET)</author>
    public class SimpleJob : IJob
    {
        private const string FolderName = "QuartzDemo";
        private const string SubfolderName = "Log";
        private static readonly ILog log = LogManager.GetLogger(typeof (SimpleJob));

        /// <summary>
        /// Called by the <see cref="IScheduler" /> when a
        /// <see cref="ITrigger" /> fires that is associated with the <see cref="IJob" />.
        /// </summary>
        public virtual void Execute(IJobExecutionContext context)
        {
            ////// This job simply prints out its job name and the
            ////// date and time that it is running
            ////JobKey jobKey = context.JobDetail.Key;
            ////log.InfoFormat("SimpleJob says: {0} executing at {1}", jobKey, DateTime.Now.ToString("r"));
            var strDirPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + FolderName + "\\" + SubfolderName;
            if (System.IO.Directory.Exists(strDirPath) == false)
            {
                System.IO.Directory.CreateDirectory(strDirPath);
            }
            string strLogPath = string.Format("{0}\\QuartzDemo_{1}_{2}.txt", strDirPath, DateTime.Now.Month.ToString(), DateTime.Now.Year.ToString());
            FileStream file = File.Exists(strLogPath) ? new FileStream(strLogPath, FileMode.Append, FileAccess.Write) : new FileStream(strLogPath, FileMode.CreateNew, FileAccess.Write);
            var sw = new StreamWriter(file);
            sw.WriteLine("{0} : NOTE # {1}", DateTime.Now, "Current Time is : {0}", DateTime.Now);
            sw.Close();
            file.Close();
        }
    }
}